package ar.edu.utn.frre.sistop.simumem.fragments;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;
import android.widget.Toast;
import ar.edu.utn.frre.sistop.simumem.R;

public class InstantChooserDialogFragment extends DialogFragment {

	private int mMax;
	private EditText mTime;

	public InstantChooserDialogFragment(int max) {
		this.mMax = max;
	}

	@SuppressLint("InflateParams")
	@Override
	public Dialog onCreateDialog(Bundle savedInstanceState) {
		// Use the Builder class for convenient dialog construction
		AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

		// Get the layout inflater
		LayoutInflater inflater = getActivity().getLayoutInflater();

		// Inflate and set the layout for the dialog
		// Pass null as the parent view because its going in the dialog layout
		View v = inflater.inflate(R.layout.dialog_jump_to, null);
		builder.setView(v);

		mTime = (EditText) v.findViewById(R.id.etTime_JumpTo);

		String title = getResources().getString(R.string.result_jump_to_title) + " [0," + mMax
				+ "]";
		builder.setTitle(title)
				.setPositiveButton(R.string.result_jump_to_go,
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog, int id) {

							}
						})
				.setNegativeButton(R.string.result_jump_to_cancel,
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog, int id) {
								// User cancelled the dialog
								InstantChooserDialogFragment.this.getDialog().cancel();
							}
						});
		// Create the AlertDialog object and return it
		// return builder.create();
		return builder.create();

	}

	@Override
	public void onStart() {
		super.onStart();
		final AlertDialog dialog = (AlertDialog) getDialog();
		dialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(
				new View.OnClickListener() {
					@Override
					public void onClick(View v) {
						validarEIr(dialog);
					}
				});
		mTime.setOnEditorActionListener(new OnEditorActionListener() {
			
			@Override
			public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
				if (actionId == EditorInfo.IME_ACTION_DONE) {
					validarEIr(dialog);
					return true;
				}
				return false;
			}
		});
	}

	protected void validarEIr(AlertDialog dialog) {
		if (tiempoValido()) {
			mListener.onDialogPositiveClick(InstantChooserDialogFragment.this, Integer.parseInt(mTime.getText().toString()));
			dialog.dismiss();
		}
	}

	protected boolean tiempoValido() {
		if (TextUtils.isEmpty(mTime.getText().toString())) {
			Toast.makeText(getActivity(), R.string.result_error_emptyField, Toast.LENGTH_SHORT)
					.show();
			return false;
		}
		if (Long.parseLong(mTime.getText().toString()) > mMax) {
			String toast = getResources().getString(R.string.result_error_outOfBounds) + " " + mMax;
			Toast.makeText(getActivity(), toast, Toast.LENGTH_SHORT).show();
			return false;
		}
		return true;
	}

	/*
	 * The activity that creates an instance of this dialog fragment must implement this interface
	 * in order to receive event callbacks. Each method passes the DialogFragment in case the host
	 * needs to query it.
	 */
	public interface InstantChooserDialogListener {
		public void onDialogPositiveClick(DialogFragment dialog, int instante);
	}

	// Use this instance of the interface to deliver action events
	InstantChooserDialogListener mListener;

	// Override the Fragment.onAttach() method to instantiate the InstantChooserDialogListener
	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		// Verify that the host activity implements the callback interface
		try {
			// Instantiate the InstantChooserDialogListener so we can send events to the host
			mListener = (InstantChooserDialogListener) activity;
		} catch (ClassCastException e) {
			// The activity doesn't implement the interface, throw exception
			throw new ClassCastException(activity.toString()
					+ " must implement InstantChooserDialogListener");
		}
	}
}
